using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BubbleBonusManager : MonoBehaviour
{
	[SerializeField]
	private GameObject _bubbleObject;

	private RectTransform _bubbleObjectTransform;

	[SerializeField]
	private GameObject _bonusPanel;

	[SerializeField]
	private GameObject _doubleMoneyPanel;

	[SerializeField]
	private GameObject _oilPanel;

	[SerializeField]
	private Text _oilKfText;

	[SerializeField]
	private GameObject _longUpgradePanel;

	[SerializeField]
	private GameObject _oilUpgradePanel;

	[SerializeField]
	private GameObject _powerUpgradePanel;

	[SerializeField]
	private GameObject _sizeUpgradePanel;

	[SerializeField]
	private GameObject _bonusHolder;

	[SerializeField]
	private RectTransform[] _movePoints;

	private Animator bonusAnimator;

	public float fuelMultiplicator = 1f;

	private List<float> fuelMultiplicators = new List<float>();

	private void Start()
	{
		fuelMultiplicators.Add(0.25f);
		fuelMultiplicators.Add(0.5f);
		fuelMultiplicators.Add(1f);
		bonusAnimator = _bonusHolder.GetComponent<Animator>();
		_bubbleObjectTransform = _bubbleObject.GetComponent<RectTransform>();
		StartCoroutine(ChangeContent());
		StartCoroutine(BubbleMove());
	}

	public void BubbleClicked()
	{
		ActiveBonus();
	}

	public void ShowBubble()
	{
		if (Preloader.LotteryRVConfig.IsEnabled)
		{
			_bubbleObject.SetActive(value: true);
		}
	}

	public void DisableBubble()
	{
		_bubbleObject.SetActive(value: false);
	}

	private void ActiveBonus()
	{
		_bonusPanel.SetActive(value: true);
		_bubbleObject.SetActive(value: false);
		_doubleMoneyPanel.SetActive(value: false);
		_oilPanel.SetActive(value: false);
		_longUpgradePanel.SetActive(value: false);
		_oilUpgradePanel.SetActive(value: false);
		_powerUpgradePanel.SetActive(value: false);
		_sizeUpgradePanel.SetActive(value: false);
		Shop shop = Object.FindObjectOfType<Shop>();
		List<BonusType> list = new List<BonusType>();
		list.Add(BonusType.Money);
		if (fuelMultiplicator <= 2f)
		{
			list.Add(BonusType.Fuel);
		}
		if (shop.CanSomeUpgrade().Count > 0)
		{
			list.Add(BonusType.Upgrade);
		}
		BonusType num = list[Random.Range(0, list.Count)];
		if (num == BonusType.Money)
		{
			Object.FindObjectOfType<Sell>().SetMultyplyer(2f);
			_doubleMoneyPanel.SetActive(value: true);
		}
		if (num == BonusType.Fuel)
		{
			float num2 = fuelMultiplicators[Random.Range(0, fuelMultiplicators.Count)];
			fuelMultiplicator += num2;
			Object.FindObjectOfType<Oil>().UpdateValues();
			_oilPanel.SetActive(value: true);
			_oilKfText.text = "X" + (num2 + 1f);
		}
		if (num == BonusType.Upgrade)
		{
			List<UpgradeType> list2 = shop.CanSomeUpgrade();
			UpgradeType upgradeType = list2[Random.Range(0, list2.Count)];
			shop.FreeUpgrade(upgradeType);
			if (upgradeType == UpgradeType.Long)
			{
				_longUpgradePanel.SetActive(value: true);
			}
			if (upgradeType == UpgradeType.Oil)
			{
				_oilUpgradePanel.SetActive(value: true);
			}
			if (upgradeType == UpgradeType.Power)
			{
				_powerUpgradePanel.SetActive(value: true);
			}
			if (upgradeType == UpgradeType.Size)
			{
				_sizeUpgradePanel.SetActive(value: true);
			}
		}
	}

	private IEnumerator ChangeContent()
	{
		int contentId = 0;
		while (true)
		{
			int num = Random.Range(0, _bonusHolder.transform.childCount);
			for (int i = 0; i < 5; i++)
			{
				if (num != contentId)
				{
					break;
				}
				num = Random.Range(0, _bonusHolder.transform.childCount);
			}
			for (int j = 0; j < _bonusHolder.transform.childCount; j++)
			{
				_bonusHolder.transform.GetChild(j).gameObject.SetActive(num == j);
			}
			contentId = num;
			if (bonusAnimator.gameObject.activeInHierarchy)
			{
				bonusAnimator.SetBool("Next", !bonusAnimator.GetBool("Next"));
			}
			yield return new WaitForSeconds(0.4f);
		}
	}

	private IEnumerator BubbleMove()
	{
		RectTransform point = _movePoints[Random.Range(0, _movePoints.Length)];
		while (Vector2.Distance(_bubbleObjectTransform.anchoredPosition, point.anchoredPosition) > 1f)
		{
			_bubbleObjectTransform.anchoredPosition = Vector2.MoveTowards(_bubbleObjectTransform.anchoredPosition, point.anchoredPosition, 0.3f);
			yield return new WaitForFixedUpdate();
		}
		StartCoroutine(BubbleMove());
	}
}
